<?php defined('_JEXEC') or die('Restricted access');

echo $this->cmealsMenu->toString();

foreach ($this->eventGroupList as $eventGroup) {
	?>
	
	<table style="min-width:50%">

	<!-- Event group header -->
	<tr><td>
	
	<p>
		<span style="font-size:large;font-weight:bold"><?php echo $eventGroup->description ?></span>
		<i>
		<?php
		$userIsCrewMember = false;
		$crewNameArray = array();
		foreach ($eventGroup->crewList as $crew) {
			if ($this->member->id == $crew->member_id) {
				$crewNameArray[] = "<b>".$crew->name."</b>";
				$userIsCrewMember = true;
			}
			else {
				$crewNameArray[] = $crew->name;
			}
		}
		echo "&nbsp;";
		JText::printf("CREW");
		echo ": ".implode(", ", $crewNameArray);
		?>
		</i>
	</p>
	<?php 
	echo "<a href=\"index.php?option=".$this->option."&view=signupreport&group_id=".$eventGroup->id."\">".JText::_("SIGNUPREPORT")."</a>";
	echo "&nbsp;&nbsp;";
	?>
	</td></tr>
	<?php 
	foreach ($eventGroup->eventList as $event) {
		//$weekday = date("l", strtotime($event->date));
		?>
		
		<!-- Event header -->
		
		<tr><td style="background-color:rgb(200,200,200)">
			<!-- Date of event -->
			<b><?php echo JHTML::date($event->date); ?></b>
			<!-- Link to user's own signup data for this event -->
			<span style="float:right">
			<?php
			// Determine user's current signup data for this event
			$participantsArray = array();
			foreach ($this->categoryList as $category) {
				$participantsArray[] = $event->categoryList[$category->id]->participantsList[$this->member->id];
			}
			$signUpDisplay = implode("/", $participantsArray);
			// Display signup data as link if event is open for signup
			// in bold if signup can no longer be changed
			// normal text if no signup has been possible yet (automatic signup displayed)
			if ($event->status == CMeals::STATUS_OPEN) {
				echo "<a href=\"".$this->baseurl."/index.php?option=".$this->option."&view=signup&group_id=".$eventGroup->id."&event_id=".$event->id."\">".$signUpDisplay."</a>";
			}
			else if ($event->status >= CMeals::STATUS_CLOSED) {
				echo "<b>".$signUpDisplay."</b>";
			}
			else {
				echo $signUpDisplay;
			}
			?>
			</span>
		</td></tr>
		
		<!-- Event description -->
		
		<tr><td style="background-color:rgb(240,240,240)">
		<?php
		if ($userIsCrewMember && $event->status != CMeals::STATUS_BILLING && $event->status != CMeals::STATUS_ARCHIVE) {
			if ($event->status == CMeals::STATUS_PLANNING || trim($event->description_html) == "") {
				$linktext = JText::_("EDIT_EVENT");
			}
			else {
				$linktext = $event->description_html;
			}
			echo "<a href=\"".$this->baseurl."/index.php?option=".$this->option."&view=editevent&group_id=".$eventGroup->id."&event_id=".$event->id."\">".$linktext."</a>";
		}
		else {
			echo $event->description_html;
		}
		?>
		</td></tr>
		<?php
		if ($event->status != CMeals::STATUS_PLANNING) { ?>
		
			<!-- Event organiser -->
		
			<tr><td style="background-color:rgb(240,240,240)">
				<?php
				JText::printf("HEAD_CHEF"); echo ": ".$event->organiser_description; 
				?>
			</td></tr>
			<tr><td style="background-color:white;font-size:small">&nbsp;</td></tr>
			<?php
		}
	}
	?>
	</table>
		
	<!-- Event group common info -->
		
	<table style="min-width:50%">
		<tr><td style="background-color:rgb(240,240,240)">
			<?php
			if (!is_null($eventGroup->accountant_description)) {
				JText::printf("ACCOUNTANT"); echo ": ".$eventGroup->accountant_description;
			}
			?>
		</td></tr>
		<tr><td style="background-color:rgb(240,240,240)">
			<?php echo $eventGroup->description_html ?>
		</td></tr>
	</table>
	<p>&nbsp;</p>
<?php
}

echo "<br/>&nbsp;";
?>

